package com.cn.demo4.servlet.admin.company;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.model.CompanyModel;
import com.cn.demo4.service.ICompanyService;
import com.cn.demo4.service.impl.CompanyServiceImpl;
import com.cn.demo4.util.LoadImg;
import com.cn.demo4.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/adminFile/company/update")
public class CompanyEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.解决乱码问题
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2.取得前台传递的值
        LoadImg.loadImg(req);
        /**
         * req在文件流的时候只能拿到一次数据，所以也不能进入过滤器
         */
        //如果没有登录则直接返回resp.getWriter().println(jsonObject);
        HttpSession session = req.getSession();
        String key = (String) req.getAttribute("loginPhone");
        String phone = (String) session.getAttribute(key);
        if (phone == null) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("result", "noLogin");
            resp.getWriter().println(jsonObject);
            return;
        }

        //3.封装数据
        CompanyModel companyModel = new CompanyModel();
        companyModel.setId(Tool.stringToInt((String) req.getAttribute("id")));
        companyModel.setName((String) req.getAttribute("name"));
        companyModel.setEmail((String) req.getAttribute("email"));
        companyModel.setPhone((String) req.getAttribute("phone"));
        companyModel.setOnline((String) req.getAttribute("online"));
        companyModel.setAddress((String) req.getAttribute("address"));
        companyModel.setAddressImg((String) req.getAttribute("addressImg"));
        companyModel.setContent((String) req.getAttribute("content"));
        companyModel.setLogo((String) req.getAttribute("img"));
        companyModel.setQrCodeIndex((String) req.getAttribute("qrCodeIndex"));
        companyModel.setQrCodeWeiXin((String) req.getAttribute("qrCodeWeiXin"));
        companyModel.setQrCodeIndexTitle((String) req.getAttribute("qrCodeIndexTiTle"));
        companyModel.setQrCodeWeiXinTitle((String) req.getAttribute("qrCodeWeiXinTitle"));
        companyModel.setRecordNumber((String) req.getAttribute("recordNumber"));
        companyModel.setRecordMessage((String) req.getAttribute("recordMessage"));
        companyModel.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        companyModel.setPhoneImg((String) req.getAttribute("phoneImg"));

        //调用服务逻辑层
        ICompanyService iCompanyService = new CompanyServiceImpl();

        //得到结果
        String result = iCompanyService.update(companyModel);
        //返回结果

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);

    }
}
